<template>
  <div>
    <el-form ref="deptForm" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments } from '@/api/departments'

export default {
  name: '',
  // 父传子
  props: {
    pid: { type: String, default: '' },
    visible: { type: Boolean },
    isEdit: { type: Boolean },
    row: {
      // type:[Object,Array]
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      managerList: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    isEdit: {
      handler(newVal, oldVal) {
        // console.log(newVal, this.row) // 根据isEdit=true表示式编辑进入的 =====将row赋值给form
        if (newVal) {
          //             深拷贝
          // 因为数据是传递过来了的，直接使用表单双向数据绑定会影响源头数据所以需要使用深拷贝===lodash

          this.form = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true // 初次进入立即执行
    }
  },
  created() {
    this.getEmp()
  },
  // 获取数据
  methods: {
    async getEmp() {
      const { data } = await getEmployeeSimple()
      this.managerList = data
    },
    submit() {
      this.$refs.deptForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async doAdd() {
      // 现将pid赋值给form中的pid
      this.form.pid = this.pid
      // 实现添加部门的接口函数调用
      const res = await addDepartments(this.form)
      console.log(res)
      if (res.code === 10000) {
        this.$emit('success')
        // 清空数据
        // this.form = {
        //   name: '',
        //   code: '',
        //   manager: '',
        //   introduce: '',
        //   pid: ''
        // }
        // 调用函数
        this.close()
        // this.$refs.deptForm.resetFields()
      }
    },
    // 编辑
    async doEdit() {
      const res = await updateDepartments(this.form)
      if (res.code === 10000) {
        return this.$emit('success') // 关闭对话框
      }
    },
    cancel() {
      this.$emit('update:visible', false)
      this.$refs.deptForm.resetFields()
    },
    // 情况数据
    close() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
